let requestUrl;

function addHandler() {
	let type = $("#_type_").combobox("getValue");

	if (type) {
		$("#type").combobox("setValue", type);
	}

	requestUrl = "/bianshenka_category/insert";

	$("#bianshenka_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#bianshenka_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/bianshenka_category/updateById";

		$("#id").val(rowData.id);
		$("#img").attr("src", rowData.image);
		$("#name").textbox("setValue", rowData.name);
		$("#type").combobox("setValue", rowData.type);
		$("#grade").numberspinner("setValue", rowData.grade);

		$("#bianshenka_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录", "warning");
	}
}

function imageHandler() {
	let rowData = $("#bianshenka_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/bianshenka_category/updateImageById";

		$("#bianshenka_id").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录", "warning");
	}
}

function deleteHandler() {
	let rowData = $("#bianshenka_list").datagrid("getSelected");

	if (rowData) {
		$.messager.confirm("提示", "是否确认删除？", function(bool) {
			if (bool) {
				ajaxGet("/bianshenka_category/deleteById", {
					id: rowData.id
				}, function(resp) {
					showMsg(resp.message);

					$("#bianshenka_list").datagrid("reload");
				}, error);
			}
		});
	} else {
		alertMsg("请选择要删除的记录", "warning");
	}
}

$(document).ready(function() {

	fetchSelect("#_type_", "/bianshenka_type/selectAll", {
		width: 150,
		editable: true,
		prompt: "-选择类型-",
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#_name_").textbox({
		width: 150,
		prompt: "输入变身卡名称"
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let type = $("#_type_").combobox("getValue");
		let name = $("#_name_").textbox("getValue");

		$("#bianshenka_list").datagrid("reload", {
			type: type,
			name: name
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#name").textbox({
		width: 150,
		required: true
	});

	$("#grade").numberspinner({
		min: 1,
		max: 9,
		width: 150,
		required: true
	});

	fetchSelect("#type", "/bianshenka_type/selectAll", {
		width: 150,
		editable: true,
		required: true,
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#bianshenka_dialog").dialog({
		title: "变身卡信息",
		width: 500,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#bianshenka_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#bianshenka_dialog").dialog("close");
						$("#bianshenka_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#bianshenka_form").form("clear");
				$("#bianshenka_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#bianshenka_list", "/bianshenka_category/upload");

	fetchUploadDialog("/bianshenka_category/updateImageById", function () {
		$("#bianshenka_list").datagrid("reload");
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#bianshenka_list").datagrid({
		url: "/bianshenka_category/selectByPage",
		method: "get",
		height: height_table,
		fitColumns: true,
		rownumbers: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}, "-", imageUpdateButton],
		columns: [[
			{field: "id", title: "编号", align: "center", hidden: true},
			{field: "name", title: "变身卡名称", align: "center", width: 100},
			{field: "type", title: "变身卡类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/bianshenka_type/selectById", {
						id: value
					}, function (resp) {
						let data = resp.data;

						$("#type_" + rowData.id).html(data.name);
					}, error);

					return "<div id='type_" + rowData.id + "'></div>";
				}
			},
			{field: "grade", title: "等级", align: "center", width: 100
				, formatter: function(value) {
					return "<div>" + value + "级</div>";
				}
			},
			{field: "image", title: "图片", align: "center", width: 100
				, formatter: function(value) {
					return "<img alt='暂无图片' height='" + size + "' src='" + value + "' />";
				}
			}
		]]
	});

});